package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;
import java.util.ArrayList;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class ProductUI
{   

    private ProductCtr proCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public ProductUI()
    {
       proCtr = new ProductCtr();
    }
    
    public void start()
    {
       
        productMenu();
    }
    
    public void productMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeProductMenu(); 
          if(choise == 1)
          {   //show person
              getProduct();
          }
          else{
              if(choise == 2){
                  createProduct();
              }  
              else{
                  if(choise == 3){
                      deleteProduct();
                  }  
                  else{
                      if(choise == 4){
                          updateProduct();
                      }
                      else{
                          if(choise == 5){
                              createOrderList();
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    public int writeProductMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Product Menu ***");
            System.out.println(" (1) Show information about product");
            System.out.println(" (2) Add product");
            System.out.println(" (3) Delete product");
            System.out.println(" (4) Update product");
            System.out.println(" (5) Print order list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            return choise;
        }
        
    //the method reads id for a person and return
    // 
    public int inputPersonsID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("input must be number - try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    public int inputBarcode()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int barcode = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write Barcode:");
            try
            {
                barcode = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return barcode;
    }
        
   
    public int inputPrice()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int price = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write Price:");
            try
            {
                price = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return price;
    }
    
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    } 
    public int inputStocknumber()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int stocknumber = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write Stocknumber:");
            try
            {
                stocknumber = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return stocknumber;
    }
     
    public int  inputMin()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int min = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write Minimum Stock:");
            try
            {
                min = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return min;
    }
    
    public int  inputMax()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int max = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write Maximum Stock:");
            try
            {
                max = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return max;
    }

    
    public String inputSupplier()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Supplier Name: ");
        String supplier = keyboard.nextLine();
        return supplier;
    }
    
    public void updateProduct()
    {
        int barcode = inputBarcode();
        int price = inputPrice();
        String name = inputName();
        int stocknumber = inputStocknumber();
        int min = inputMin();
        int max = inputMax();
        String supplier = inputSupplier();
        proCtr.updateProduct(barcode, price, name, stocknumber, min, max, supplier);
    }
    
    
    private void createProduct()
    {
        int barcode = inputBarcode();
        int price = inputPrice();
        String name = inputName();
        int stocknumber = inputStocknumber();
        int min = inputMin();
        int max = inputMax();
        String supplier = inputSupplier();
        proCtr.createProduct(barcode, price, name, stocknumber, min, max, supplier);
    }
        
    
    private void getProduct()
    {
        boolean ok = false;
        while(!ok)
        {
            try
            {
                int barcode = inputBarcode();
                Product product = proCtr.findProduct(barcode);
                System.out.println("Barcode: " + product.getBarcode());
                System.out.println("Price: " + product.getPrice());
                System.out.println("Name: " + product.getName());
                System.out.println("Stocknumber: " + product.getStocknumber());
                System.out.println("Minimum stock: " + product.getMin());
                System.out.println("Maximum stock: " + product.getMax());
                System.out.println("Supplier: " + product.getSupplier());
                ok = true;
                pause();
            }
            catch(Exception e)
            {
                System.out.println("The barcode doesn't exist - try again");
            }
        }
    }
    
    private void deleteProduct()
    {
        boolean ok = false;
        while(!ok)
        {
            try
            {
                int barcode = inputBarcode();
                proCtr.deleteProduct(barcode);
            }
            catch(Exception e)
            {
                System.out.println("The barcode doesn't exist - try again");
            }
        }
    }
    
    public void createOrderList()
    {
        int index = 0;
        ArrayList<Product> orderList = proCtr.createOrderList();
        while(index < orderList.size())
        {
            Product product = orderList.get(index);
            System.out.println("Barcode: " + product.getBarcode());
            System.out.println("Price: " + product.getPrice());
            System.out.println("Name: " + product.getName());
            System.out.println("Stocknumber: " + product.getStocknumber());
            System.out.println("Minimum stock: " + product.getMin());
            System.out.println("Maximum stock: " + product.getMax());
            System.out.println("Supplier: " + product.getSupplier());
            index++;
        }
        pause();
    }
    
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
